


const MongoClient = require('mongodb').MongoClient;
const DB_CONN_STR = 'mongodb://lanyue_user:finfosoft123@121.42.253.149:19907/lanyue';

const flowDB = {
    updateFlow:function(conditions,update,insertFlow,dbname,dbFlow){//update  更新的内容
        let updateData = function(db, callback) {
            //连接到表
            let collection = db.collection(dbname);
            //更新数据
            let whereStr = conditions;
            let updateStr = {$set: update};
            collection.update(whereStr,updateStr, function(err, result) {
                if(err)
                {
                    console.log('Error:'+ err);
                    return;
                }
                callback(result);
            });
        };
        let insertData = function(db, callback) {
            //连接到表
            var collection = db.collection(dbFlow);
            //插入数据
            var data = insertFlow;
            collection.insert(data, function(err, result) {
                if(err)
                {
                    console.log('Error:'+ err);
                    return;
                }
                callback(result);
            });
        }

        MongoClient.connect(DB_CONN_STR, function(err, db) {

            updateData(db, function(result) {
                console.log("流量更新成功！");
                // console.log(result);
                db.close();
            });
            insertData(db, function(result) {
                console.log("流量数组更新成功！");
                console.log(result);
                db.close();
            });
        });
    }
}



module.exports = flowDB